import { Component, OnInit } from '@angular/core';
import { Hero } from '../hero';
import { HEROES } from '../mock-heroes';
import { HeroService } from '../hero.service';

@Component({
  selector: 'app-heroes',
  templateUrl: './heroes.component.html',
  styleUrls: ['./heroes.component.css']
})
export class HeroesComponent implements OnInit {
  heros: any;
  hero: Hero = {
    id: 1,
    name: 'Windstorm'
  };
  constructor(private heroservice: HeroService) { }

  ngOnInit() {
    this.getHeroes();
  }
  getHeroes(): void {
    this.heroservice.getHeroes().subscribe(
      heros => {
        this.heros = heros;
      }
    );
  }
  add(name: string): void {
    name = name.trim();
    if (!name) { return; }
    this.heroservice.addHero({ name } as Hero)
      .subscribe(hero => {
        this.heros.push(hero);
      });
  }
  delete(hero: Hero): void {
    this.heros = this.heros.filter(h => h !== hero);
    this.heroservice.deleteHero(hero).subscribe();
  }
}
